表格套件,本篇主要介紹使用Client及Server table的基礎
註冊的方式:
import {ServerTable, ClientTable, Event} from 'vue-tables-2';
Vue.use(ClientTable); //Client table
Vue.use(ServerTable); //Server table
或直接引用JS檔,
<script src="https://cdn.jsdelivr.net/npm/vue-tables-2@1.4.70/dist/vue-tables-2.min.js"></script>
<script>
Vue.use(VueTables.ClientTable); //Client table
Vue.use(VueTables.ServerTable); //Server table
</script>
可額外指定參數:
Vue.use(ClientTable, [options = {}], [useVuex = false], [theme = 'bootstrap3'], [template = 'default']);
例如:
Vue.use(ClientTable, {}, false, 'bootstrap3', 'default');
| Option | Description | Type | Default | 
|---|---|---|---|
| options | Object | {} | |
| useVuex | 是否使用Vuex做狀態管理 | Boolean | false | 
| theme | 選擇CSS framework,可為 bootstrap3,bootstrap4,bulma | String | 'bootstrap3' | 
| template | 選擇HTML樣板,例如 default,footerPagination | String | 'default' | 
<v-client-table ref="myTable" :data="tableData" :columns="columns" :options="options"></v-client-table>
const FOO_DATA = [
    {id: 1, name:'Luke Skywalker',gender:'male', img:'https://goo.gl/KEUxHN'},
    ...
];
export default {
  name: "app",
  data() {
    return {
      columns: ["id", "name", "gender"],
      tableData: [],
      options: {}
    };
  },
  created() {
    this.tableData = FOO_DATA;
  }
};
/assets/demo1.gif)
若要取得目前已篩選的表格資料,可使用ref屬性以取得
<v-client-table ref="myTable" :data="tableData" :columns="columns" :options="options"></v-client-table>
//Get the filtered table data on current page
let showFilteredCurrentPageData =
      this.$refs.myTable.filteredData;
//Get the filtered table data on all pages
let showFilteredData = 
      this.$refs.myTable.allFilteredData;
若資料需ajax後端的資料,則可改使用Server table。
支援以下三種ajax框架:
<v-server-table url="http://localhost:3000/starwars" :columns="columns" :options="options"></v-server-table>
其中url指定了要取得資料的網址,其回傳的JSON必須包含data(Table data,格式為Array)及count(總筆數,格式為Number)。
例如:
"data":[
    {"id": 1,"name":"Luke Skywalker","gender":"male", "img":"https://goo.gl/KEUxHN"},
    //...skip the other 8 records
    {"id": 10,"name":"Darth Sidious","gender":"male","img":"https://goo.gl/QJiJWx"},
],
"count":100
當定義了url時,vue-tables-2將自動送出如下之request url:
http://localhost:3000/starwars?query=&limit=10&ascending=1&page=1&byColumn=0&orderBy=name
| Param | Description | 
|---|---|
| query | 查詢(篩選)的關鍵字 | 
| page | 當前頁數 | 
| orderBy | 排序的欄位 | 
| ascending | 1: 升序,0: 降序 | 
| byColumn | 當在 options設定了filterByColumn:true(可BY欄位做篩選),此值為 1,否則為 0 | 
另若不使用上述三種ajax的框架或需客製送出request的方法,可透過設定options中的requestFunction方法。
<v-server-table :columns="columns" :options="options"></v-server-table>
options: {
    requestFunction: function (params) {
        return axios.get("http://localhost:3000/starwars", {
            params: params
        });
    }
}
/assets/demo2.gif)